package Chapter1.sort;

public class MaoPaoSort {
    public static void main(String[] args) {
        int nums[] = {1, 5, 3, 2, 4};
        sort2(nums);
        for (int num : nums) {
            System.out.println(num);
        }
    }

    public static void sort(int[] nums) {
        int len = nums.length;
        int index = 0;
        while (index < len) {
            for (int j = len - 1; j > index; j--) {
                if (nums[j] < nums[index]) {
                    int temp = nums[index];
                    nums[index] = nums[j];
                    nums[j] = temp;
                }
            }
            index++;
        }
    }

    public static void sort3(int[] nums) {
        int len = nums.length;
        int n = 0;
        while (n < len) {
            int min = nums[n];
            int minIndex = n;
            for (int i = n + 1; i < len; i++) {
                if (min > nums[i]) {
                    min = nums[i];
                    minIndex = i;
                }
            }
            if (minIndex == n) {
                n++;
                continue;
            }
            for (int j = minIndex; j > n; j--) {
                nums[j] = nums[j - 1];
            }
            nums[n] = min;
            n++;
        }
    }

    public static void sort2(int[] nums) {
        int len = nums.length;
        int index = 0;
        while (index < len) {
            boolean swap = false;
            for (int j = len - 1; j > index; j--) {
                if (nums[j] < nums[j - 1]) {
                    int temp = nums[j];
                    nums[j] = nums[j - 1];
                    nums[j - 1] = temp;
                    swap = true;
                }
            }
            if (!swap) {
                break;
            }
            index++;
        }
    }
}
